function btnAdd(){
		window.location.href = "add.jsp";
}

function details(tableId){
	$.layer({
	    type: 2,
	    title: false,
		iframe: {src: 'detail.jsp?tableId='+tableId},
	    area : ['500px' , '300px'],
	    success : function(){	
	    }
	});
}

function clearTxt(){
	$("#txtTableName").val('');
	$("#txtCreateUser").val('');
}

var pageIndex = 0;     //页面索引初始值  
var pageSize = 10;     //每页显示条数初始化，修改显示条数，修改这里即可
var querypageIndex = 0;     //页面索引初始值  
var querypageSize = 10;
function btnQuery(){
	var conditonCount = "";
	$.ajax({
    	type : "GET",
		dataType: 'json',
		contentType: "application/json",
		async: false, 
        url: ctx + "/sti.do?action=8&tableName=" +$("#txtTableName").val() + "&createUser="+$("#txtCreateUser").val() ,      //提交到一般处理程序请求数据  
        success: function(data) {
        	conditonCount = data;
        	if(count == 0){
        		$("#Pagination").hide();
        	}else{
        		$("#Pagination").show();
        	}
        }  
     });  
	 $("#Pagination").pagination(conditonCount, {  
         callback: PageCallbackForQuery,  
         prev_text: '上一页',       //上一页按钮里text  
         next_text: '下一页',       //下一页按钮里text  
         items_per_page: querypageSize,  //显示条数  
         num_display_entries: 5,    //连续分页主体部分分页条目数  
         current_page: querypageIndex,   //当前页索引  
         num_edge_entries: 5        //两侧首尾分页条目数  
     });    

     //翻页调用  
     function PageCallbackForQuery(index, jq) { 
     	headTable();
     	InitTableForQuery(index);  
     }  
	
}

$(function() {          
    //分页，PageCount是总条目数，这是必选参数，其它参数都是可选  
    $("#Pagination").pagination(getCount(), {  
        callback: PageCallback,  
        prev_text: '上一页',       //上一页按钮里text  
        next_text: '下一页',       //下一页按钮里text  
        items_per_page: pageSize,  //显示条数  
        num_display_entries: 5,    //连续分页主体部分分页条目数  
        current_page: pageIndex,   //当前页索引  
        num_edge_entries: 5        //两侧首尾分页条目数  
    });    

    //翻页调用  
    function PageCallback(index, jq) { 
    	headTable();
        InitTable(index);  
    }  
    
    function getCount(){
    	var count = "";
    	alert(ctx + "/sti.do?action=4");
    	$.ajax({
        	type : 'get',
			dataType: 'json',
			contentType: "application/json",
    		async: false, 
            url: ctx + "/sti.do?action=4",      //提交到一般处理程序请求数据  
            success: function(data) { 
            	count = data;
            	if(count == 0){
            		$("#Pagination").hide();
            	}else{
            		$("#Pagination").show();
            	}
            }  
         });  
    	
    	return count;
    }
}); 


//请求数据  
function InitTable(pageIndex) { 
    $.ajax({
    	type : 'get',
		dataType: 'json',
		contentType: "application/json",
		async: false, 
        url: ctx + '/sti.do?action=3&pageIndex=' + (pageIndex + 1) + '&pageSize=' + pageSize,      //提交到一般处理程序请求数据  
        success: function(data) {
			$.each(data.list, function(index, value){
				$("#tableHeader").after(getTableInfoForTable(value.tableId, value.tableName, value.database, value.createUser, value.createTime, value.modifyTime, value.tableDesc));
				});
            }  
        });              
    }  
    
  //请求数据  
function InitTableForQuery(querypageIndex) {     	
    $.ajax({
    	type : 'get',
		dataType: 'json',
		contentType: "application/json",
		async: false, 
        url: ctx + "/sti.do?action=7&tableName=" +$("#txtTableName").val() + "&createUser="+$("#txtCreateUser").val() +"&pageIndex=" + (querypageIndex + 1) + "&pageSize=" + querypageSize,      //提交到一般处理程序请求数据  
        success: function(data) {
			$.each(data.list, function(index, value){
				$("#tableHeader").after(getTableInfoForTable(value.tableId, value.tableName, value.database, value.createUser, value.createTime, value.modifyTime, value.tableDesc));
			});
        }  
    });              
}  
  
function headTable(){
	var string = "";
	string+="<tr id=\"tableHeader\">";
	string+="<td style=\"color: #999;\">表名</td>";
	string+="<td style=\"color: #999;\">所属库</td>";
	string+="<td style=\"color: #999;\">创建者</td>";
	string+="<td style=\"color: #999;\">表说明</td>";
	string+="<td style=\"color: #999;\">创建时间</td>";
	string+="<td style=\"color: #999;\">最后修改时间</td>";
	string+="<td style=\"color: #999;\">操作</td>";
	string+="</tr>";
	
	$("#tableData").html(string);
}

function designs(tableId){
	window.location.href="design.jsp?tableId=" +tableId;
}

function getTableInfoForTable(tableId, tableName, database, createUser, createTime, modifyTime, tableDesc){
	var str = "";
	str+="<tr>";
	str+="<td style=\"color: #444;\">" + tableName + "</td>";
	str+="<td style=\"color: #444;\">" + database + "</td>";
	str+="<td style=\"color: #444;\">" + createUser + "</td>";
	str+="<td style=\"color: #444;\">"
	+ (tableDesc.length > 6 ? tableDesc.substring(0,6) + "..." : tableDesc) + "</td>";
	str+="<td style=\"color: #444;\">" + createTime + "</td>";
	str+="<td style=\"color: #444;\">" + modifyTime + "</td>";
	str+="<td style=\"color: #444;\"><img src=\"../../../image/design.jpg\" alt=\"设计\" style=\"cursor:pointer;\" onclick=\"designs('"+tableId+"');\"/></a>"+
	"<a><img alt=\"编辑\" onclick=\"edits('"+tableId+"');\" style=\"cursor:pointer;\"  src=\"../../../image/edit.png\"/></a>";
	str+="<a><img src=\"../../../image/detail.png\" alt=\"详细\" style=\"cursor:pointer;\" onclick=\"details('"+tableId+"');\"/>" + 
	"<a><img src=\"../../../image/delete.jpg\" alt=\"删除\" style=\"cursor:pointer;\" onclick=\"deletes('"+tableId+"');\"/></a></td>";
	str+="</tr>";
	
	return str;
}  

function edits(tableId){
	$.layer({
	    type: 2,
	    title: false,
		iframe: {src: 'edit.jsp?tableId='+tableId},
	    area : ['500px' , '300px'],
	    success : function(){	
	    }
	});
}

function btnImport(){
	$.layer({
	    type: 2,
	    title: false,
		iframe: {src: 'sqlimport.jsp?'},
	    area : ['500px' , '200px'],
	    success : function(){	
	    }
	});
}

function deletes(tableId){
	layer.confirm('你真的确定要删除吗?',function(index){
	    layer.close(index);
	    $.ajax({
        	type : 'post',
			dataType: 'json',
			contentType: "application/json",
    		async: false, 
            url: ctx + '/sti.do?action=2&tableId=' + tableId,  
            success: function(data) {    
				if(data == "1"){
					layer.alert('删除成功', 8);
				}
            }  
        }); 
	});    	
}

function home(){
	window.open("http://www.meileche.com");
}